package com.huishu.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.pojo.dto.HotWordsDTO;
import com.huishu.goods.pojo.model.HotWordsDO;
import com.huishu.goods.service.HotWordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author generator@ljj
 * @since 2023-04-14
 */
@RestController
@RequestMapping("/hot-words")
@Slf4j
public class HotWordsController {

    @Autowired
    private HotWordsService hotWordsService;

    @GetMapping("/find-all")
    public Result findAll() {
        LambdaQueryWrapper<HotWordsDO> queryWrapper = Wrappers.lambdaQuery(HotWordsDO.class)
                .orderByAsc(HotWordsDO::getSort);
        return Result.success(hotWordsService.list(queryWrapper));
    }

    @PostMapping("/pageList")
    public Result pageList(@RequestBody BasePage basePage) {
        return hotWordsService.pageList(basePage);
    }

    @PostMapping("/create")
    public Result create(@RequestBody HotWordsDTO hotWordsDTO) {
        return hotWordsService.handle(hotWordsDTO, 1);
    }

    @PostMapping("/update")
    public Result modify(@RequestBody HotWordsDTO hotWordsDTO) {
        return hotWordsService.handle(hotWordsDTO, 3);
    }

    @DeleteMapping("/delete/{id}")
    public Result modify(@PathVariable Long id) {
        HotWordsDO hotWordsDO = hotWordsService.getById(id);
        HotWordsDTO hotWordsDTO = BeanUtils.copyProperties(hotWordsDO, HotWordsDTO.class);
        return hotWordsService.handle(hotWordsDTO, 2);
    }

    @GetMapping("/set-default/{id}")
    public Result setDefault(@PathVariable Long id){
        return hotWordsService.setDefault(id);
    }
}
